import requests
import time
# import json
# 1 1#主机非电气量
# 2 2#主机非电气量
# 3 3#主机非电气量
# 4 4#主机非电气量
# 5 电气量
# 6 电气设备记录
# 7 变频机组电气量
# 8 空
# 9 空
# 10 扬压力
# 月报
# http://192.168.100.10:8081/LLJBBXT/averageyueServlet
# 参数：nianyue: 2024-03
# http://192.168.100.10:8081/LLJBBXT/averagenianServlet 
# 参数：nian: 2024
# http://192.168.100.10:8081/LLJBBXT/LLJHISServlet
# 参数：
# startTime: 2024-03-22 00:00:00
# endTime: 2024-03-22 23:59:59
# STCD: 1
# 0:00	20.6	25.2	21.3	21.1	21.2	19.7	21.2	19.6	0.2	0.8	1.0	1.0	7.0	8.0	1.0	1.0	1.0	0.0	0.0	0.0	18.3	0.0	18.65	16.02	16.07	2.58	0.00	0.00	-3.9	0.0	0.00

def get_data(STCD):
    '''
    获取数据库数据
    STCD: 10为测压管
    返回json
    '''
    data = {
        "startTime": f"{time.strftime('%Y-%m-%d %H:00:00',time.localtime())}",
        "endTime": f"{time.strftime('%Y-%m-%d %H:59:59',time.localtime())}",
        "STCD": STCD
    }
    try:
        resp = requests.post("http://192.168.100.10:8081/LLJBBXT/LLJHISServlet",data=data)
        # print(resp.json())
        return resp.json()
    except Exception as e:
        return str(e)

def tosendcontent(resp_text):
    '''
    转换成发送内容
    '''
    result = []
    for r in resp_text:
        result.append( f"{r['tagID']}={r['value']}")
    result = f"({'&'.join(result)})"
    print(result)
    return result
        
if __name__ == "__main__":
    # print(time.strftime('%Y-%m-%d %H:00:00',time.localtime()))
    tosendcontent(get_data(10))
    # while 1:
    #     now = int(time.time())
    #     if (now - 5) % 3600 == 0:
    #         get_cyg(10)
    #         # time.sleep(1)
    #     time.sleep(1)